package com.maomao.maomaochong_i.application;

import android.app.Activity;
import android.app.Application;

import com.maomao.maomaochong_i.MainActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by maomao on 2017/6/18.
 */

public class MyApplication extends Application {
    //创建一个存储程序中所有Activity的list
    private List<Activity> mList = new LinkedList<Activity>();
    //定义静态类实例
    private static MyApplication instance;

    public MyApplication(){}

    //异步的静态类方法
    public synchronized static MyApplication getInstance(){
        //如果类实例为null 就创建它
        if (instance == null){
            instance = new MyApplication();
        }
        return instance;
    }

    //添加 Activity 进list
    public void addActivity(Activity activity){
        mList.add(activity);
    }

    //关闭包括登录界面前的界面,不含MainActivity
    public void finish(){
        try{
            for(Activity activity : mList){
                if (activity != null && !(activity instanceof MainActivity))
                    activity.finish();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
//        finally {
//            //关闭当前活动的 Activity
//            System.exit(0);
//        }
    }

    //退出所有Activity
    public void exit(){
        try{
            for(Activity activity : mList){
                if (activity != null)
                    activity.finish();
            }
        } catch(Exception e){
            e.printStackTrace();
        } finally {
            //关闭当前活动的 Activity
            System.exit(0);
        }
    }

    public void onLowMemory(){
        super.onLowMemory();
        System.gc();
    }

}
